﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.InventoryItemCategory;
using WindowsFormsModel.BusinessObjects;
using Utils;
using Infragistics.Win.UltraWinTree;
using Infragistics.Win.UltraWinEditors;
using WindowsFormsPresenter.InventoryItem;

namespace WindowsFormApplication.FormCategoryTreeView
{
    public partial class frmMaterialList : frmBaseTreeView, IInventoryItemCategoriesView, IInventoryItemsView
    {
        #region Presenter

        private InventoryItemCategoriesPresenter _inventoryItemCategoriesPresenter;
        private InventoryItemsPresenter _inventoryItemsPresenter;

        #endregion

        private static int totalPage = 0;
        private static int currentPage = 0;
        private static bool isMaterial = true;

        #region Field

        public IList<InventoryItemModel> InventoryItems
        {
            set
            {
                var inventoryItems = value;
                grdMain.DataSource = inventoryItems;

                for (int i = 0; i < grdMain.DisplayLayout.Bands[0].Columns.Count; i++) { grdMain.DisplayLayout.Bands[0].Columns[i].Hidden = true; }

                grdMain.DisplayLayout.Bands[0].Columns["InventoryItemCode"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["InventoryItemName"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["UnitPrice"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["SalesPrice"].Hidden = false;
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Hidden = false;

                grdMain.DisplayLayout.Bands[0].Columns["InventoryItemCode"].Header.Caption = "Mã nguyên liệu";
                grdMain.DisplayLayout.Bands[0].Columns["InventoryItemName"].Header.Caption = "Tên nguyên liệu";
                grdMain.DisplayLayout.Bands[0].Columns["UnitPrice"].Header.Caption = "Giá nhập";
                grdMain.DisplayLayout.Bands[0].Columns["SalesPrice"].Header.Caption = "Giá bán";
                grdMain.DisplayLayout.Bands[0].Columns["IsActive"].Header.Caption = "Được sử dụng";

                grdMain.DisplayLayout.Bands[0].Columns["UnitPrice"].Format = "#,###,###,###,###";
                grdMain.DisplayLayout.Bands[0].Columns["SalesPrice"].Format = "#,###,###,###,###";
            }
        }

        public IList<InventoryItemCategoryModel> InventoryItemCategories
        {
            set
            {
                DataSet ds = new DataSet();
                ds = value.AsDataSet(TableNameTree);

                ds.Relations.Add(RelationName, ds.Tables[TableNameTree].Columns[TreePrimaryKey], ds.Tables[TableNameTree].Columns[TreeParentKey], false);
                treeMain.DataSource = ds.Tables[TableNameTree];

                UltraTreeNode node = new UltraTreeNode();
                node = treeMain.TopNode;

                if (treeMain.Nodes.Count > 0)
                {
                    while (node != null)
                    {
                        node.DataKey = node.Cells[TreePrimaryKey].Value;
                        node.Key = node.DataKey.ToString();
                        node = node.NextVisibleNode;
                    }
                }
                docTree.ControlPanes[0].Text = "Danh sách loại nguyên liệu";
            }
        }

        #endregion

        protected override void InitData()
        {
            try
            {
                _inventoryItemCategoriesPresenter.DisplayActive(true);
                totalPage = _inventoryItemsPresenter.GetTotalPage(isMaterial);
                _inventoryItemsPresenter.DisplayByPageAndBranch(currentPage, isMaterial);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void NextPage()
        {
            if (currentPage < totalPage)
            {
                currentPage = currentPage + 1;
                InitData();
            }
        }

        protected override void PreviousPage()
        {
            if (currentPage > 0)
            {
                currentPage = currentPage - 1;
                InitData();
            }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (grdMain.ActiveRow.Index >= 0)
                {
                    string inventoryItemId = grdMain.ActiveRow.Cells[TablePrimaryKey].Value.ToString();
                    new InventoryItemPresenter(null).Delete(inventoryItemId);
                    currentPage = 0;
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void FilterObject(string keyValue)
        {
            try
            {
                totalPage = _inventoryItemsPresenter.GetTotalPageByCategory(keyValue);
                _inventoryItemsPresenter.DisplayByPageAndCategory(currentPage, keyValue);
                toolbarsManager.Tools["lblPage"].SharedProps.Caption = "Số trang " + (currentPage + 1) + "/ " + (totalPage + 1);
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmMaterialList()
        {
            InitializeComponent();

            _inventoryItemCategoriesPresenter = new InventoryItemCategoriesPresenter(this);
            _inventoryItemsPresenter = new InventoryItemsPresenter(this);
        }
    }
}
